package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleMapper;
import com.itheima.pojo.CategoryMenu;
import com.itheima.pojo.Feedback;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import com.itheima.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private IdWorker idWorker;

    @Override
    public Page<Role> findAll(Map<String, Object> roleVO) {
        Integer currentPage = Integer.parseInt(roleVO.get("currentPage").toString());
        Integer pageSize = Integer.parseInt(roleVO.get("pageSize").toString());
        PageHelper.startPage(currentPage,pageSize);
        String keyword = roleVO.get("keyword").toString();
        roleVO.put("roleName",keyword);
        Example example=this.createExample(roleVO);
        Page<Role> rolePage = (Page<Role>) roleMapper.selectByExample(example);
        return rolePage;
    }

    //更新角色信息
    @Override
    @Transactional
    public void edit(Map<String, Object> roleVO) {
        //将map转为comment对象
        String roleStr = JSON.toJSONString(roleVO);
        Role role = JSON.parseObject(roleStr, Role.class);
        //将修改时间添加到对象
        role.setUpdateTime(new Date());
        //修改操作
        roleMapper.updateByPrimaryKey(role);
    }

    //删除角色信息
    @Override
    @Transactional
    public void delete(Map<String, Object> roleVO) {
        String uid = roleVO.get("uid").toString();
        roleMapper.deleteByPrimaryKey(uid);
    }

    //新增角色信息
    @Override
    @Transactional
    public void add(Map<String, Object> roleVO) {
        //将map转为comment对象
        String roleStr = JSON.toJSONString(roleVO);
        Role role = JSON.parseObject(roleStr, Role.class);
        //对时间格式化
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        try {
            role.setUid(String.valueOf(idWorker.nextId()));
            role.setCreateTime(simpleDateFormat.parse(format));
            role.setUpdateTime(simpleDateFormat.parse(format));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //修改操作
        roleMapper.insertSelective(role);
    }

    @Override
    public Role findById(String uid) {
        Role role = roleMapper.selectByPrimaryKey(uid);
        return role;
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Role.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 唯一uid
            if(searchMap.get("uid")!=null && !"".equals(searchMap.get("uid"))){
                criteria.andLike("uid","%"+searchMap.get("uid")+"%");
            }
            // 角色名
            if(searchMap.get("roleName")!=null && !"".equals(searchMap.get("roleName"))){
                criteria.andEqualTo("roleName",searchMap.get("roleName"));
            }

            // 创建时间
            if(searchMap.get("createTime")!=null){
                criteria.andEqualTo("createTime",searchMap.get("createTime"));
            }

            // 更新时间
            if(searchMap.get("updateTime")!=null){
                criteria.andEqualTo("updateTime",searchMap.get("updateTime"));
            }

            // 状态
            if(searchMap.get("status")!=null){
                criteria.andEqualTo("status",searchMap.get("status"));
            }

            // 角色介绍
            if(searchMap.get("summary")!=null && !"".equals(searchMap.get("summary"))){
                criteria.andLike("summary","%"+searchMap.get("summary")+"%");
            }

            // 角色管辖的菜单的UID
            if(searchMap.get("categoryMenuUids")!=null && !"".equals(searchMap.get("categoryMenuUids"))){
                criteria.andEqualTo("categoryMenuUids",searchMap.get("categoryMenuUids"));
            }

        }
        return example;
    }
}
